package com.star.boxiaotong.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.boxiaotong.Constant.GoodsConstant;
import com.star.boxiaotong.common.ErrorCode;
import com.star.boxiaotong.module.domain.Goods;
import com.star.boxiaotong.exception.BusinessException;
import com.star.boxiaotong.mapper.GoodsMapper;
import com.star.boxiaotong.module.request.AddTagsRequest;
import com.star.boxiaotong.module.request.GetLowPriceRequest;
import com.star.boxiaotong.service.GoodsService;
import jakarta.annotation.Resource;
import javafx.util.Pair;
import org.apache.commons.lang3.StringUtils;


import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
* @author hp
* @description 针对表【goods(商品表)】的数据库操作Service实现
* @createDate 2023-06-08 14:14:14
*/



@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;


    @Override
    public String addBrandTags(AddTagsRequest addTagsRequest) {

        String brandName = addTagsRequest.getBrandName();
        //1.校验参数
        //1.1 品牌不能为空
        if(StringUtils.isBlank(brandName)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "品牌不能为空");
        }

        //1.2 商品名称不能为空
        String goodsName = addTagsRequest.getGoodsName();
        if(StringUtils.isBlank(goodsName)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品不能为空");
        }

        //1.3 如果 品牌不为空且品牌为其它，则系列为其它，插入数据库，并且返回标签
        Goods goods = null;
        if (GoodsConstant.DEFAULT_GOODS_TAGS.equals(brandName)) {
            goods = new Goods();
            BeanUtils.copyProperties(addTagsRequest, goods);
            goods.setTag(GoodsConstant.DEFAULT_GOODS_TAGS);
            goodsMapper.insert(goods);
            return GoodsConstant.DEFAULT_GOODS_TAGS;
        }

        //2.匹配关键字
        //2.1 匹配第一关键字
        String fieldOne = addTagsRequest.getFieldOne();

        String mapsName = addTagsRequest.getMapsName();
        if (StringUtils.isNotBlank(fieldOne) && goodsName.contains(fieldOne) ) {
            goods = new Goods();
            BeanUtils.copyProperties(addTagsRequest, goods);
            goods.setTag(mapsName);
            goodsMapper.insert(goods);
            return mapsName;
        }

        //2.2 匹配第二、第三、第四关键字， 若匹配成功则，系列名称则为映射值
        String fieldTwo = addTagsRequest.getFieldTwo();
        String fieldThree = addTagsRequest.getFieldThree();
        String fieldFour = addTagsRequest.getFieldFour();
        // 2.3 如果字段为空或者匹配则为一个条件为true

        boolean condition1 = StringUtils.isBlank(fieldTwo) || goodsName.contains(fieldTwo);
        boolean condition2 = StringUtils.isBlank(fieldThree) || goodsName.contains(fieldThree);
        boolean condition3 = StringUtils.isBlank(fieldFour) || goodsName.contains(fieldFour);

        if (condition1 && condition2 && condition3) {
            goods = new Goods();
            BeanUtils.copyProperties(addTagsRequest, goods);
            goods.setTag(mapsName);
            goodsMapper.insert(goods);
            return mapsName;
        }

        //2.3 若匹配不成功，系列名称则为品牌名 + 其它; 插入数据库，并返回
        goods = new Goods();
        BeanUtils.copyProperties(addTagsRequest, goods);
        goods.setTag(brandName + GoodsConstant.DEFAULT_GOODS_TAGS);
        goodsMapper.insert(goods);
        return goods.getTag();
    }




    @Override
    public Double extractNumber(String goods_name) {

        //1.分别校验是否匹配毫升
        Pattern patternML = Pattern.compile("(\\d+|\\d+\\.\\d+|\\.\\d+) *(ml|Ml|ML|mL|[\\u6beb][\\u5347])");
        Matcher matcherML = patternML.matcher(goods_name);

        if (matcherML.find()) {
            String str = matcherML.group(0);
            Pattern pattern1 = Pattern.compile("\\d+(?:\\.\\d+)?");
            Matcher matcher1 = pattern1.matcher(str);
            if (matcher1.find()) {
                String result = matcher1.group(0);
                return Double.valueOf(result);
            }

        }

        //2.是否匹配升
        Pattern patternL = Pattern.compile("(\\d+|\\d+\\.\\d+|\\.\\d+) *(l|L|[\\u5347])");
        Matcher matcherL = patternL.matcher(goods_name);

        if (matcherL.find()) {
            String str = matcherL.group(0);
            Pattern pattern1 = Pattern.compile("\\d+(?:\\.\\d+)?");
            Matcher matcher1 = pattern1.matcher(str);
            if (matcher1.find()) {
                String result = matcher1.group(0);
                return Double.valueOf(result)* 1000 ;
            }

        }
        return 0d;
    }

    @Override
    public Double getLowPrice(GetLowPriceRequest getLowPriceRequest) {

        //1.检验价格是否合理
        Double price = getLowPriceRequest.getPrice();
        if (price == null || price < 0d) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"价格不存在或不合理");
        }
        //2.检验是否有促销信息，如果没有则返回原价
        String information = getLowPriceRequest.getInformation();
        if (StringUtils.isBlank(information)) {
            return price;
        }
        //3.如果有则进行计算
        //3.1 获得优惠信息，用正则提取优惠信息的满减
        String regex = "满\\d+减\\d+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(information);

        //3.2获取所有优惠的信息
        List<Pair<Double, Double>> list = new ArrayList<>();
        while (matcher.find()) {
            String text = matcher.group();
            int index1 = text.indexOf("满") + 1;
            int index2 = text.indexOf("减");
            Double fullAmount = Double.valueOf(text.substring(index1, index2));
            Double minusAmount = Double.valueOf(text.substring(index2 + 1));
            list.add(new Pair<Double, Double>(fullAmount,minusAmount));
        }

        //3.3依次取出计算最小值
        int size = list.size();
        Double result = price;
        for (int i = 0; i < size; i++) {
            Pair<Double, Double> Pair = list.get(i);
            Double fullAmount = Pair.getKey();
            Double minusAmount = Pair.getValue();
            int count = (int) (fullAmount / price) + 1;
            double v = (price * count - minusAmount) / count;
             if (v < result) {
                 result = v;
             }

        }

        return result;
    }


    /**
     * 尝试提取其中的所有数字
     * @param goods_name
     * @return
     */
    private List<Double> extractNumber1(String goods_name) {

        //1.书写正则表达式来提取商品中的信息
        Pattern pattern = Pattern.compile("(\\d+|\\d+\\.\\d+|\\.\\d+) *(ml|Ml|ML|mL|l|L|[\\u5347]|[\\u6beb][\\u5347])");
        Matcher matcher = pattern.matcher(goods_name);
        //2.对数据进行处理封装成结合,如果匹配的是毫升答问返回元数据，
        //3.如果返回的是升，则要变成毫升

        List<Double> result = new ArrayList<>();
        while(matcher.find()) {
            String str = matcher.group();
            Pattern pattern1 = Pattern.compile("\\d+(?:\\.\\d+)?");
            Matcher matcher1 = pattern1.matcher(str);
            if (matcher1.find()) {
                String s = matcher1.group(0);
                result.add(Double.valueOf(s));
            }

        }

        //4.如果没有单量，则直接返回 0
        return result;

    }






}




